import requests
import json

def city(adcode):
    url2 = "https://ditu.amap.com/service/weather"
    parser = {
        "adcode": adcode
    }
    response = requests.get(url2, headers=headers, params=parser)
    ret = response.text
    ret = json.loads(ret)
    ret = ret["data"]["data"][0]["forecast_data"][0]
    weather = ret["weather_name"]
    max_temp = ret["max_temp"]
    min_temp = ret["min_temp"]

    print("天气为：{};温度为：{}/{}℃".format(weather, min_temp, max_temp))


headers = {
"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62"
}
url1 = "https://ditu.amap.com/service/cityList?version=2021122215"

city_name = input("输入你要查找的城市天气：")

response = requests.get(url1,headers=headers)
data = json.loads(response.text)["data"]["cityByLetter"]
a=0
for v in data.values():
    for i in v:
        if i["name"]==city_name:
            adcode=i["adcode"]
            city(adcode)
            a=1
            break
if a == 0:
    print("你要查找的城市：”{}“不存在".format(city_name))